package com.cykj.v_m_sprintboot.controller;
import com.cykj.v_m_sprintboot.bean.*;
import com.cykj.v_m_sprintboot.service.TbInputService;
import com.cykj.v_m_sprintboot.service.TbOutService;
import com.cykj.v_m_sprintboot.service.TbWarehouseService;
import com.cykj.v_m_sprintboot.tool.GetTime;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class InputController {

    @Autowired
    private TbInputService tbInputService;
    //仓库入库获取供应商的方法
    @RequestMapping("getSupplier")
    @ResponseBody
    public Map<String,Object> getSupplier(){
        List<TbInput> supplierList=tbInputService.getSupplier(1);
        Map<String,Object> map=new HashMap<>();
        map.put("supplierList",supplierList);
        return map;
    }
    //仓库入库根据供货商获取商品种类的方法
    @RequestMapping("getGoodsType")
    @ResponseBody
    public Map<String,Object> getGoodsType(@RequestBody TbInput tbInput){
        System.out.println("后台传过来的数据为："+tbInput.getSupplierId());
        int supplierId= (int) tbInput.getSupplierId();
        Map<String,Object> map=new HashMap<>();
       String[] goodsList=tbInputService.getGoodsType(supplierId).split(",");
        map.put("goodsList",goodsList);
        return map;
    }
    //仓库入库根据商品的种类获取商品的方法
    @RequestMapping("getGoodsName")
    @ResponseBody
    public Map<String,Object> getGoodsName(@RequestBody TbInput tbInput){
        System.out.println("后台传过来的商品的种类为："+tbInput.getGoodsListType());
        Map<String,Object> map=new HashMap<>();
        List<TbInput> goodsList=tbInputService.getGoodsName(tbInput.getGoodsListType());
        map.put("goodsList",goodsList);
        return map;
    }
    //获取入库时仓库的名称
    @RequestMapping("getWarehouseName")
    @ResponseBody
    public Map<String,Object> getWarehouseName(){
        Map<String,Object> map=new HashMap<>();
        List<TbInput> warehouseNameList=tbInputService.getWarehouseName(1);
        map.put("warehouseNameList",warehouseNameList);
        return map;
    }
    //数据入库的所有数据添加到数据库

    @RequestMapping("addWarehouseData")
    @ResponseBody
    public String addWarehouseData(@RequestBody TbInput tbInput) {
        System.out.println("提交入库数据为：" + tbInput.getInputTime() + "," + tbInput.getSupplierId() + ","
                + tbInput.getGoodsListId() + "," + tbInput.getGoodsName() + ","
                + tbInput.getGoodsNum() + "," + tbInput.getGoodsUnit() + ","
                + tbInput.getWarehouseId());
//        //把商品的类型装换为字符串
//        tbInput.setGoodsType(tbInputService.getGoodsTypeByGoodsListId((int)tbInput.getGoodsListId()));
        System.out.println(tbInput.getGoodsType()+"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
        //查看入库时时间是否重复
        TbInput tbs = new TbInput(tbInput.getInputTime());
        TbInput tbs1 = tbInputService.selectAddGoodsId(tbs);
        if (tbs1 == null) {
            //商品数据添加到商品表中
            boolean goods = tbInputService.addGoods(tbInput);
            if (goods == true) {
                //调取当场存入商品表的Id
                TbInput tp = tbInputService.selectAddGoodsId(tbInput);
                if (tp != null) {
                    tbInput.setGoodsId(tp.getGoodsId());
                    //入库单数据添加到入库表中
                    boolean input = tbInputService.addInputData(tbInput);
                    if (input == true) {
                        //通过仓库查询仓库
                        int stateId = tbInputService.selectWarehouse((int) tp.getWarehouseId());
                        //改变仓库的存储情况
                        if (stateId == 1) {
                            tbInputService.updateWarehouseNumState((int) tp.getWarehouseId(), 2);
                        }
                        //入库后仓库的最后修改时间也要改变
                        tbInputService.updateWarehouseModifyTime((int)tbInput.getWarehouseId(),GetTime.getTime());
                        //入库成功之后数据添加到仓库商品列表当中以便后期数量的统计
                        tbInput.setModifyTime(GetTime.getTime());
                        tbInput.setGoodsUnit("件");
                        TbWarehouseListInf tbWarehouseListInf =tbInputService.selectGoodsInf(tbInput);
                        if (tbWarehouseListInf==null){
                            tbInputService.addTbWarehouseListInf(tbInput);
                        }else{
                            double count=tbInputService.countTbWarehouseListInf(tbInput);
                            tbInput.setGoodsNum(tbInput.getGoodsNum()+count);
                            tbInputService.updateTbWarehouseListInfNum(tbInput);
                        }

                        return "入库成功";
                    }
//                    return "入库数据添加到入库表失败";
                }
//                return "存入商品Id失败";
            }
            return "入库失败";
        }
        return "入库的时间不能一致，请从新选取时间";

    }
    //获取入库清单
    @RequestMapping("getWarehousing")
    @ResponseBody
    public Map<String,Object> getWarehousing(@RequestBody TbInput pageInf){
        pageInf.setStateListId(1);
        System.out.println("当前页："+pageInf.getPage()+",几条数据为一页："+pageInf.getSize());
        RowBounds row =new RowBounds((pageInf.getPage()-1)*pageInf.getSize(),pageInf.getSize());
        List<TbInput> warehousing=tbInputService.getWarehousing(pageInf,row);
        Map<String,Object> map=new HashMap<>();
        map.put("count",tbInputService.warehousingCount(pageInf));
        map.put("warehousing",warehousing);
        return map;
    }
    //删除入库单
    @RequestMapping("delInput")
    @ResponseBody
    public String delInput(@RequestBody TbInput tbInput){
        System.out.println("tbInputId:"+tbInput.getInputId()+","+"stateListId:"+tbInput.getStateListId()
    +","+"warehouseId:"+tbInput.getWarehouseId());
        boolean flag=tbInputService.delInput((int)tbInput.getInputId(),tbInput.getStateListId());
        if (flag==true){
            //入库单删除之后也要改变改变仓库的最后时间
            tbInputService.updateWarehouseModifyTime((int)tbInput.getWarehouseId(),GetTime.getTime());
            //移除之后仓库商品列表的商品数量数要减去删除数据表中的出库数量
            tbInput.setModifyTime(GetTime.getTime());
            double count=tbInputService.countTbWarehouseListInf(tbInput);
            tbInput.setGoodsNum(count-tbInput.getGoodsNum());
            if (tbInput.getGoodsNum()>=0) {
                tbInputService.updateTbWarehouseListInfNum(tbInput);
                return "移除成功";
            }
        }
        return "移除失败";

    }

}
